/*
 * Copyright (C) 2018 wxyz <hyhjwzx@126.com>
 * This program can be distributed under the terms of the GNU GPL Version 2.
 * See the file LICENSE.
 *
 */

package ren.wxyz.study.euler.hp02;

import ren.wxyz.study.euler.base.IProblem;
import ren.wxyz.study.euler.util.ConsoleHelper;
import ren.wxyz.study.euler.util.FileHelper;
import ren.wxyz.study.euler.util.PathHelper;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 第 11 题
 *
 * @auther wxyz 2018-04-21_14:30
 * @since 1.0
 */
public class P011 implements IProblem {

    /**
     * 四个数的最大的乘积
     */
    private int maxProduct = 0;

    /**
     * 四个数
     */
    private int[] fourNum;

    @Override
    public void main(String[] args) throws Throwable {
        Path filePath = Paths.get(PathHelper.getClassPath(P011.class), "hp02/P011/input.txt");
        int[][] grid = FileHelper.readNumberGrid(filePath, " ");

        for (int i = 0; i <= grid.length - 4; i++) {
            for (int j = 0; j <= grid[i].length - 4; j++) {
                // 从左到右
                compareProduct(grid[i][j], grid[i][j + 1], grid[i][j + 2], grid[i][j + 3]);
                // 从上到下
                compareProduct(grid[i][j], grid[i + 1][j], grid[i + 2][j], grid[i + 3][j]);
                // 从左上到右下
                compareProduct(grid[i][j], grid[i + 1][j + 1], grid[i + 2][j + 2], grid[i + 3][j + 3]);
                // 从左下到右上
                compareProduct(grid[i + 3][j], grid[i + 2][j + 1], grid[i + 1][j + 2], grid[i][j + 3]);
            }
        }

        ConsoleHelper.printf("四个数的乘积是 %1$d * %2$d * %3$d * %4$d = %5$d \r\n",
                fourNum[0], fourNum[1], fourNum[2], fourNum[3], maxProduct);
    }

    /**
     * 比较给定数的乘积
     *
     * @param nums 多个数
     */
    private void compareProduct(int... nums) {
        int product = 1;

        for (int num : nums) {
            product *= num;
        }

        if (product > maxProduct) {
            maxProduct = product;
            fourNum = nums;
        }
    }
}
